package org.component.item 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import org.component.TabBar;
	import com.peachcastle.core.Display;
	
	public class TabBarItem extends Display 
	{
		
		private var _index:int = 0;
		
		private var _txt:TextField = null;
		
		private var _subject:TabBar = null;
		
		private var _format:TextFormat = null;
		
		public function TabBarItem(ui:MovieClip = null) 
		{
			super(ui);
		}
		override protected function initialization():void 
		{
			super.initialization();
			
			_format = new TextFormat();
			assest.buttonMode = true;
			_txt = assest["txt"];
			_txt.selectable = false;
			_txt.mouseEnabled = false;
			
			_index = parseInt(assest.name.substr(assest.name.length - 1));
		}
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			assest.addEventListener(MouseEvent.MOUSE_DOWN, onAssestDown);
			assest.addEventListener(MouseEvent.ROLL_OVER, onAssestOver);
			assest.addEventListener(MouseEvent.ROLL_OUT,onAssestOut);
		}
		
		public function getSubject(sub:TabBar = null):void
		{
			_subject = sub;
			
		}
		private function onAssestDown(event:MouseEvent):void
		{
			if (select) return;
			assest.gotoAndStop("down");
			_txt.setTextFormat(_format);
			_subject.selectItem = this;
		}
	
		private function onAssestOver(event:MouseEvent):void
		{
			if (select) return;
			_format.color = _subject.color["over"];
			assest.gotoAndStop("over");
			_txt.setTextFormat(_format);
		}
		private function onAssestOut(event:MouseEvent):void
		{
			if (select) return;
			_format.color = _subject.color["out"];
			assest.gotoAndStop("out");
			_txt.setTextFormat(_format);
		}
		
		
		private var _select:Boolean = false;
		public function get select():Boolean { return _select; }
		public function set select(value:Boolean):void
		{
			_select = value;
			
			if (value)
			{
				_format.color = _subject.color["select"];
				assest.gotoAndStop("select");
				
				assest.mouseEnabled = false;
				
			}else
			{
				_format.color = _subject.color["out"];
				assest.gotoAndStop("out");
				assest.mouseEnabled = true;
			}
			_txt.setTextFormat(_format);
		}
		
		public function get index():int { return _index; }
		public function set index(value:int):void { _index = value};
		
		public function get label():String { return _txt.text; }
		public function set label(value:String):void
		{
			_txt.text = value;
		}
	}

}